package common

// NumberT 泛型约束，支持的数值类型
type NumberT interface {
	int8 | int16 | int32 | int64 | int | byte | uint16 | uint32 | uint64 | uint | float32 | float64
}

// Max 返回两个数值中的较大值。
func Max[T NumberT](a, b T) T {
	if a > b {
		return a
	}
	return b
}

// Min 返回两个数值中的较小值。
func Min[T NumberT](a, b T) T {
	if a < b {
		return a
	}
	return b
}

// ConvertInt64 将任意支持的类型转换为 int64。
func ConvertInt64(a interface{}) int64 {
	switch v := a.(type) {
	case string:
		return NewString(v).ToInt64V()
	case int8:
		return int64(v)
	case int16:
		return int64(v)
	case int32:
		return int64(v)
	case int64:
		return v
	case int:
		return int64(v)
	case byte:
		return int64(v)
	case uint16:
		return int64(v)
	case uint32:
		return int64(v)
	case uint64:
		return int64(v)
	case uint:
		return int64(v)
	case float32:
		return int64(v)
	case float64:
		return int64(v)
	default:
		return 0
	}
}

// ConvertInt32 将任意支持的类型转换为 int32。
func ConvertInt32(a interface{}) int32 {
	switch v := a.(type) {
	case string:
		return int32(NewString(v).ToInt64V())
	case int8:
		return int32(v)
	case int16:
		return int32(v)
	case int32:
		return v
	case int64:
		return int32(v)
	case int:
		return int32(v)
	case byte:
		return int32(v)
	case uint16:
		return int32(v)
	case uint32:
		return int32(v)
	case uint64:
		return int32(v)
	case uint:
		return int32(v)
	case float32:
		return int32(v)
	case float64:
		return int32(v)
	default:
		return 0
	}
}
